package zju.ccnt.rest.service.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
import sun.jvm.hotspot.utilities.Assert;
import zju.ccnt.exception.BaseWebApplicationException;

/**
 * Created by zha on 14/11/18.
 */
@ResponseStatus(value = HttpStatus.CONFLICT, reason = "Item has already exists.")
public class NewItemConflictException extends BaseWebApplicationException{
    private String[] uniqueProperty;
    private String[] value;
    private String itemClassName;
    public NewItemConflictException(String itemClassName, String[] uniqueProperty, String[] value){
        super(HttpStatus.CONFLICT, "Item has already exists.");
        Assert.that(uniqueProperty.length == value.length, "uniqueProperty's length does not match value's");

        this.itemClassName = itemClassName;
        this.uniqueProperty = uniqueProperty;
        this.value = value;
    }

    public String getSummary(){
        StringBuilder builder = new StringBuilder();
        for (int i = uniqueProperty.length - 1; i >= 0; i--){
            builder.append(uniqueProperty[i]).append(": ").append(value[i]).append(", ");
        }
        builder.append(" has already exists for " + itemClassName);
        return builder.toString();
    }

    @Override
    public String getMessage() {
        return getSummary();
    }
}
